//
//  SeaPictureViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/5/19.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "SeaPictureViewController.h"
#import "YHYPicCollectionViewCell.h"



@interface SeaPictureViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain)UICollectionView *myCollectionView;
@property (assign, nonatomic)NSInteger pageC;
//@property (assign, nonatomic)NSInteger preIndex;
@property (nonatomic,retain)UIPageControl *pageControl;
@property (nonatomic,retain)UILabel *nameL;

@end

@implementation SeaPictureViewController



- (void)viewWillAppear:(BOOL)animated {
 //   [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
 //   [UINavigationBar appearance] setBackgroundImage:<#(nullable UIImage *)#> forBarMetrics:<#(UIBarMetrics)#>
   
//    self.navigationController.navigationBarHidden = NO;
//    self.navigationController.navigationBar.translucent = NO;
    
     self.tabBarController.tabBar.hidden=YES;
     self.navigationController.navigationBar.translucent = YES;
   
    
}

-(void) viewWillDisappear:(BOOL)animated
{
   // self.tabBarController.tabBar.hidden=NO;
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
     self.title = [NSString stringWithFormat:@"%d/%ld",1,_seaArray.count];
   
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"quxiao"] style:UIBarButtonItemStylePlain target:self action:@selector(tapImageV:)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];

}
- (void)Back:(UIButton *)btu{
//    [self dismissViewControllerAnimated:YES completion:^{
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"arr" object:_seaArray];
//        NSLog(@"tongzhi1%ld",_seaArray.count);
//        
//    }   ];
    
    [self.navigationController popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"arr" object:_seaArray];
        NSLog(@"tongzhi1%ld",_seaArray.count);

}
- (void)tapImageV:(UIButton *)bti{
    NSLog(@"xxxxxxxxx%ld",(long)self.pageC);
    NSString *title = NSLocalizedString(@"提示", nil);
    NSString *message = NSLocalizedString(@"删除图片", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
    NSString *otherButtonTitle = NSLocalizedString(@"确定", nil);
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //  NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
        
        
    }];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (_seaArray.count > 1) {
            [_seaArray removeObjectAtIndex:(long)self.pageC];
            
            self.title = [NSString stringWithFormat:@"%ld/%ld",(long)self.pageC + 1,_seaArray.count];
            
            [_myCollectionView reloadData];
            
        }
        else if (_seaArray.count == 1){
            // NSLog(@"sdfsdfdsf");
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经最后一张了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            
            [alter show];
        }
        
        
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    

}

- (void)creatView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);

    
    _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:layout];
    // _myCollectionView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_myCollectionView];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.pagingEnabled = YES;
    _myCollectionView.showsHorizontalScrollIndicator = FALSE; // 去掉滚动条
    [_myCollectionView registerClass:[YHYPicCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //水平滑动
   [_myCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.indexNumber inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
      [_myCollectionView reloadData];
    

}

#pragma  mark - 滑动conllectionView控制segment
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger mark =self.myCollectionView.contentOffset.x/self.view.frame.size.width;
    // self.seg.selectedSegmentIndex = mark;
    //  self.pageC = &(mark);
    self.pageC = mark;
    self.title = [NSString stringWithFormat:@"%ld/%ld",mark + 1,_seaArray.count];
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 139 /2 * AUTO_HEIGHT);
}

//-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
//    return YES;
//}
//



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   return _seaArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ce = @"cell";
    
    YHYPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ce forIndexPath:indexPath];
  //  cell.myimage = [_seaArray objectAtIndex:indexPath.row];
   // [cell bringSubviewToFront:self.view];
    
    
    
    cell.myBackWhiteView.image = [_seaArray objectAtIndex:indexPath.row];
      return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
